//
//  RGMeView.m
//  RedGrass
//
//  Created by wukong on 2018/11/27.
//  Copyright © 2018年 hongcaosp. All rights reserved.
//

#import "RGMeView.h"
#import "RGMeVedioCell.h"
#import "RGMeDynamicsCell.h"
#import "HoverViewFlowLayout.h"
#import "LoadMoreControl.h"
#import "RGMeInterfaceBulider.h"
#import "RGRecommendVideoModel.h"
#import <AFNetworking/AFNetworking.h>
#import "GKDYVideoPlayer.h"

#define  itemBaseTag  100000


@interface RGMeView()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,UIScrollViewDelegate,UserInfoDelegate,OnTabTapActionDelegate,GKDYVideoPlayerDelegate>

@property (nonatomic, strong) UICollectionView                 *collectionView;
@property (nonatomic, assign) CGFloat                          itemWidth;
@property (nonatomic, assign) CGFloat                          itemHeight;
@property (nonatomic, strong) LoadMoreControl                  *loadMore;
@property (nonatomic, assign) NSInteger                        pageIndex;
@property (nonatomic, assign) NSInteger                        pageSize;

@property (nonatomic, assign) NSInteger                        tabIndex;
@property (nonatomic,strong) RGUserInfoModel *model;
@property (nonatomic,strong) NSMutableArray *worksArray;
@property (nonatomic,strong) NSMutableArray *dynamicsArray;
@property (nonatomic,strong) NSMutableArray *loversArray;

@property (nonatomic,strong) UILabel *customNavTitleLabel;
@property (strong, nonatomic) GKDYVideoPlayer *vedioPlayer;
@property (assign, nonatomic) NSInteger  itemIndex;


@end

@implementation RGMeView


-(instancetype) initWithFrame:(CGRect)frame{
    if(self = [super initWithFrame:frame]){
        _itemWidth = (KScreenWidth - (CGFloat)(((NSInteger)(KScreenWidth)) % 3) ) / 3.0f - 1.0f;
        _itemHeight = _itemWidth * 1.35f;
        _itemIndex = itemBaseTag;
        _pageIndex = 1;
        [self addSubview:self.collectionView];
        [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(UIEdgeInsetsZero);
        }];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onNetworkStatusChange:) name:KNotificationNetWorkStateChange object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loversArrayChanged:) name:KNotificationLoversArrayChanged object:nil];
        
        [self addSubview:self.customNavTitleLabel];
        [_customNavTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(kStatusBarHeight + 5);
            make.centerX.mas_equalTo(self.mas_centerX).mas_offset(0);
        }];
        
        _loadMore = [[LoadMoreControl alloc] initWithFrame:CGRectMake(0, kUserInfoHeaderHeight, KScreenWidth, 50) surplusCount:15];
        [_loadMore startLoading];
        __weak __typeof(self) wself = self;
        _pageSize = PageSize;
        [_loadMore setOnLoad:^{
            [wself loadData:wself.userId pageIndex:wself.pageIndex pageSize:wself.pageSize];
        }];
        [_collectionView addSubview:_loadMore];
    }
    return self;
}



//网络状态发送变化
-(void)onNetworkStatusChange:(NSNotification *)notification {
    if(AFNetworkReachabilityManager.sharedManager.reachable) {
        if(self.model == nil) {
            [self loadUserData:_userId];
        }
        if(self.worksArray.count == 0 && self.dynamicsArray.count == 0 && self.loversArray.count == 0) {
            [self loadData:_userId pageIndex:_pageIndex pageSize:_pageSize];
        }
    }
}

- (void)loversArrayChanged:(NSNotification *)notification {
    //如果是在喜欢tab才处理通知，否则不需要。因为点击喜欢tab会拉取数据
    if (_tabIndex != 2) {
        return ;
    }

    RGRecommendVideoModel *model = notification.object;
    NSPredicate *preicate = [NSPredicate predicateWithFormat:@"SELF.ID == %i", model.ID];
    NSArray *result = [self.loversArray filteredArrayUsingPredicate:preicate];
    if (model.isLike) {
        if (result.count == 0) {
            [self.loversArray insertObject:model atIndex:0];
        }
    }else {
        if (result.count > 0) {
            [self.loversArray removeObjectsInArray:result];
        }
    }
    [self.collectionView reloadSections:[NSIndexSet indexSetWithIndex:1]];
}


//喜欢列表里面的数据喜欢状态改变时，需要重新刷新数据源
- (void)reloadDataWhenNeedUpdateData:(NSInteger)index{
    _tabIndex = index;
    _pageIndex = 1;
    [UIView setAnimationsEnabled:NO];
    [self.collectionView performBatchUpdates:^{
        [self.worksArray removeAllObjects];
        [self.dynamicsArray removeAllObjects];
        [self.loversArray removeAllObjects];
        
        if([self.collectionView numberOfItemsInSection:1]) {
            [self.collectionView reloadSections:[NSIndexSet indexSetWithIndex:1]];
        }
    } completion:^(BOOL finished) {
        [UIView setAnimationsEnabled:YES];
        
        [self.loadMore reset];
        [self.loadMore startLoading];
        
        [self loadData:self.userId pageIndex:self.pageIndex pageSize:self.pageSize];
        
    }];
}

//OnTabTapDelegate
- (void)onTabTapAction:(NSInteger)index {
    if(_tabIndex == index){
        return;
    }
    _tabIndex = index;
    _pageIndex = 1;
    
    [UIView setAnimationsEnabled:NO];
    [self.collectionView performBatchUpdates:^{
        [self.worksArray removeAllObjects];
        [self.dynamicsArray removeAllObjects];
        [self.loversArray removeAllObjects];
        
        if([self.collectionView numberOfItemsInSection:1]) {
            [self.collectionView reloadSections:[NSIndexSet indexSetWithIndex:1]];
        }
    } completion:^(BOOL finished) {
        [UIView setAnimationsEnabled:YES];
        
        [self.loadMore reset];
        [self.loadMore startLoading];
        
        [self loadData:self.userId pageIndex:self.pageIndex pageSize:self.pageSize];
        
    }];
}

-(void) setIsShowHeaderViewBtnView:(BOOL)isShowHeaderViewBtnView{
    _isShowHeaderViewBtnView = isShowHeaderViewBtnView;
}

//初始化数据
-(void)loadData:(NSInteger)userId {
    _userId = userId;
//    if(_model == nil) {
        [self loadUserData:_userId];
//    }
    if(_worksArray.count == 0 && _dynamicsArray.count == 0 && _loversArray.count == 0) {

        [self loadData:_userId pageIndex:_pageIndex pageSize:_pageSize];
    }
}

- (void)reloadUserSectionWithModel:(RGMessageContactModel *)model {
    _model = model;
    [self.collectionView reloadSections:[NSIndexSet indexSetWithIndex:0]];
}

- (void)loadUserData:(NSInteger)userId {
    HttpRequest *quest = [HttpRequest buildRequest:url_userInfo];
    NSDictionary *params = [RGMeInterfaceBulider buildUserInfo:userId];
    AA_WEAK_MAKE(self) weakSelf = self;
    [quest post:params success:^(id data) {
//        [weakSelf dismissLoading];
        RGUserInfoModel *model = [[RGMessageContactModel alloc] initWithDictionary:[data objectForKey:@"userInfo"] error:nil];
        if (model) {
            
            RGUserInfoModel *user = [RGUserInfoModel getUserInfo];
            if(user.ID == userId){
                model.token = user.token;
                [RGUserInfoModel storeUserInfo:model];
            }else{
                if([weakSelf.meViewDelegate respondsToSelector:@selector(updatePersonalPageWithNickName:meFollowmTa:)]){
                    NSDictionary *dic = [data objectForKey:@"userInfo"];
                    BOOL meFollowmTa = [[dic objectForKey:@"meFollowmTa"] boolValue];
                    [weakSelf.meViewDelegate updatePersonalPageWithNickName:model.nickname meFollowmTa:meFollowmTa];
                }
            }
            

            weakSelf.model = model;
            weakSelf.customNavTitleLabel.text = model.nickname;
            [self.collectionView reloadSections:[NSIndexSet indexSetWithIndex:0]];
        }
    } fail:^(NSInteger code, NSString *message) {
        [weakSelf.meViewDelegate showTextMessageWithMessage:message];
    }];
}

- (void)loadData:(NSInteger)userId pageIndex:(NSInteger)pageIndex pageSize:(NSInteger)pageSize {
    AA_WEAK_MAKE(self) weakSelf = self;
    HttpRequest *quest;
    if(_tabIndex == 0) {
        quest = [HttpRequest buildRequest:url_userWorks];
        NSDictionary *params = [RGMeInterfaceBulider buildUserWorks:userId WithPageNumber:self.pageIndex pageSize:PageSize];
        [quest post:params success:^(id data) {
            if (weakSelf.tabIndex != 0) {
                return ;
            }
            weakSelf.pageIndex ++;
            NSArray *array = [RGRecommendVideoModel arrayOfModelsFromDictionaries:[data objectForKey:@"list"] error:nil];
            [UIView setAnimationsEnabled:NO];
            [weakSelf.collectionView performBatchUpdates:^{
                [weakSelf.worksArray addObjectsFromArray:array];
                NSMutableArray<NSIndexPath *> *indexPaths = [NSMutableArray array];
                for(NSInteger row = weakSelf.worksArray.count - array.count; row<weakSelf.worksArray.count; row++) {
                    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:1];
                    [indexPaths addObject:indexPath];
                }
                [weakSelf.collectionView insertItemsAtIndexPaths:indexPaths];
            }completion:^(BOOL finished) {
                [UIView setAnimationsEnabled:YES];
            }];
            [weakSelf.loadMore endLoading];
            if(array.count < pageSize) {
                [weakSelf.loadMore loadingAll];
            }
        } fail:^(NSInteger code, NSString *message) {
            [weakSelf.loadMore loadingFailed];
        }];
    }else if (_tabIndex == 1) {
        quest = [HttpRequest buildRequest:url_userDynamic];
        NSDictionary *params = [RGMeInterfaceBulider buildUserDynamic:userId WithPageNumber:self.pageIndex pageSize:PageSize];
        [quest post:params success:^(id data) {
            if(weakSelf.tabIndex != 1) {
                return;
            }
            weakSelf.pageIndex ++;
            NSArray *array = [RGRecommendVideoModel arrayOfModelsFromDictionaries:[data objectForKey:@"list"] error:nil];
            [UIView setAnimationsEnabled:NO];
            [weakSelf.collectionView performBatchUpdates:^{
                [weakSelf.dynamicsArray addObjectsFromArray:array];
                NSMutableArray<NSIndexPath *> *indexPaths = [NSMutableArray array];
                for(NSInteger row = weakSelf.dynamicsArray.count - array.count; row<weakSelf.dynamicsArray.count; row++) {
                    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:1];
                    [indexPaths addObject:indexPath];
                }
                [weakSelf.collectionView insertItemsAtIndexPaths:indexPaths];
            }completion:^(BOOL finished) {
                [UIView setAnimationsEnabled:YES];
                if(weakSelf.pageIndex == 1){
                    [weakSelf playVedioWithFirstIndexWhenLoadSuccess];
                }
            }];
            [weakSelf.loadMore endLoading];
            if(array.count < pageSize) {
                [weakSelf.loadMore loadingAll];
            }
        } fail:^(NSInteger code, NSString *message) {
            [weakSelf.loadMore loadingFailed];
        }];
    }else if (_tabIndex == 2) {
        quest = [HttpRequest buildRequest:url_userLoves];
        NSDictionary *params = [RGMeInterfaceBulider buildUserLoves:userId WithPageNumber:self.pageIndex pageSize:PageSize];
        [quest post:params success:^(id data) {
            if(weakSelf.tabIndex != 2) {
                return;
            }
            weakSelf.pageIndex ++;
            NSArray *array = [RGRecommendVideoModel arrayOfModelsFromDictionaries:[data objectForKey:@"list"] error:nil];
            [UIView setAnimationsEnabled:NO];
            [weakSelf.collectionView performBatchUpdates:^{
                [weakSelf.loversArray addObjectsFromArray:array];
                NSMutableArray<NSIndexPath *> *indexPaths = [NSMutableArray array];
                for(NSInteger row = weakSelf.loversArray.count - array.count; row<weakSelf.loversArray.count; row++) {
                    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:1];
                    [indexPaths addObject:indexPath];
                }
                [weakSelf.collectionView insertItemsAtIndexPaths:indexPaths];
            }completion:^(BOOL finished) {
                [UIView setAnimationsEnabled:YES];
            }];
            [weakSelf.loadMore endLoading];
            if(array.count < pageSize) {
                [weakSelf.loadMore loadingAll];
            }
        } fail:^(NSInteger code, NSString *message) {
            [weakSelf.loadMore loadingFailed];
        }];
    }
}

//UICollectionViewDataSource Delegate
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 2;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.section == 0 && kind == UICollectionElementKindSectionHeader) {
        RGUserInfoHeaderView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"RGUserInfoHeaderView" forIndexPath:indexPath];
        header.isShowBtnView = _isShowHeaderViewBtnView;
        AA_WEAK_MAKE(self)weakSelf = self;
        [header setActionBtnClick:^(NSInteger tag) {
            if (weakSelf.meViewDelegate && [weakSelf.meViewDelegate respondsToSelector:@selector(actionBtnClickWithView:Tag:)]) {
                [weakSelf.meViewDelegate actionBtnClickWithView:weakSelf Tag:tag];
            }
        }];
        header.delegate = self;
//        header.slideTabBar.delegate = self;
        header.tabIndex = _tabIndex;
        if (_model) {
            header.model = _model;
        }
        _userInfoHeader = header;
        return header;
    }
    return [UICollectionReusableView new];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    NSInteger num = 0;
    if(section == 1) {
        switch (self.tabIndex) {
            case 0:
                num = self.worksArray.count;
                break;
            case 1:
                num = self.dynamicsArray.count;
                break;
            case 2:
                num = self.loversArray.count;
                break;
            default:
                break;
        }
    }
    return num;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (self.tabIndex == 1) {
        RGMeDynamicsCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"RGMeDynamicsCell" forIndexPath:indexPath];
        cell.tag = itemBaseTag + indexPath.row;
        cell.vedioModel = [self.dynamicsArray objectAtIndex:indexPath.row];
        AA_WEAK_MAKE(self)weakSelf = self;
        [cell setActionBtnClickBlock:^(NSInteger tag) {
            if(tag == kFocusListFocusTag){//点赞
                if([weakSelf.meViewDelegate respondsToSelector:@selector(favoriteVedioWithView:chosedIndexPath: vedioModel:)]){
                    [weakSelf.meViewDelegate favoriteVedioWithView:weakSelf chosedIndexPath:indexPath vedioModel:weakSelf.dynamicsArray[indexPath.row]];
                }
            }else if (tag == kFocusListCommentTag){//评论
                if([weakSelf.meViewDelegate respondsToSelector:@selector(comementVedioWithView:chosedIndexPath: vedioModel:)]){
                    [weakSelf.meViewDelegate comementVedioWithView:weakSelf chosedIndexPath:indexPath vedioModel:weakSelf.dynamicsArray[indexPath.row]];
                }
            }else if (tag == kFocusListShareTag){//g转发
                if([weakSelf.meViewDelegate respondsToSelector:@selector(shareVedioWithView:chosedIndexPath: vedioModel:)]){
                    [weakSelf.meViewDelegate shareVedioWithView:weakSelf chosedIndexPath:indexPath vedioModel:weakSelf.dynamicsArray[indexPath.row]];
                }
            }else if (tag == kFocusListPlayBtnTag){//点击了播放按钮
                [weakSelf updateCellVedioPlayStatusWithIndexPath:indexPath];
                
            }else if (tag == kFocusListUserIconTag){//点击了图像
                
            }
        }];
        return cell;
    }else {
        RGMeVedioCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"RGMeVedioCell" forIndexPath:indexPath];
        if (self.tabIndex == 0) {
            RGRecommendVideoModel *model = [self.worksArray objectAtIndex:indexPath.row];
            cell.model = model;
            cell.showCount = model.playCount;
            cell.isShowLikeImg = NO;
        }else {
            RGRecommendVideoModel *model = [self.loversArray objectAtIndex:indexPath.row];
            cell.model = model;
            cell.showCount = model.likeCounts;
            cell.isShowLikeImg = YES;
        }
        return cell;
    }
}

//UICollectionViewDelegate Delegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if(indexPath.section == 1 && _tabIndex == 0){
        
        if([self.meViewDelegate respondsToSelector:@selector(cellItemChosedWithView:chosedIndexPath:dataList:chosedTabIndex:)]){
            [self.meViewDelegate cellItemChosedWithView:self chosedIndexPath:indexPath dataList:_worksArray chosedTabIndex:_tabIndex];
        }
    }else if (indexPath.section == 1 && _tabIndex == 2){
        if([self.meViewDelegate respondsToSelector:@selector(cellItemChosedWithView:chosedIndexPath:dataList:chosedTabIndex:)]){
            [self.meViewDelegate cellItemChosedWithView:self chosedIndexPath:indexPath dataList:_loversArray chosedTabIndex:_tabIndex];
        }
    }
}

//UICollectionFlowLayout Delegate
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    if(section == 0) {
        return CGSizeMake(KScreenWidth, kUserInfoHeaderHeight);
    }
    return CGSizeZero;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
    return CGSizeZero;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (self.tabIndex == 1) {
        RGRecommendVideoModel *model = [self.dynamicsArray objectAtIndex:indexPath.row];
        return  CGSizeMake(KScreenWidth, model.cellHeight);
    }else {
        return  CGSizeMake(_itemWidth, _itemHeight);
    }
}

-(void) stopPlayingWhenCurrentVCIsNoteVisiable{
    if([_vedioPlayer isPlaying]){
        [_vedioPlayer pausePlay];
    }
}

-(void) playVedioWithFirstIndexWhenLoadSuccess{
    //    _itemIndex = 0;
    if(_tabIndex != 1){
        return;
    }
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        RGMeDynamicsCell *cell = (RGMeDynamicsCell *)[self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
//        if(cell){
            self.itemIndex = cell.tag;
            [self.vedioPlayer removeVideo];
            [self.vedioPlayer playVideoWithView:cell.vedioGroudView url:cell.vedioUrl];
            if([self.meViewDelegate respondsToSelector:@selector(updateVedioPlayCountWithVedioId:)]){
                [self.meViewDelegate updateVedioPlayCountWithVedioId:cell.vedioModel.ID];
            }
//        }
   
    });
}

//更新当前cell的视频播放状态
-(void) updateCellVedioPlayStatusWithIndexPath:(NSIndexPath *)path{
    if((path.row + itemBaseTag)!= _itemIndex){//点击的不是当前的播放视频的cell
        return;
    }
    if(_vedioPlayer.status == GKDYVideoPlayerStatusPlaying){
        [_vedioPlayer pausePlay];
        
    }else if (_vedioPlayer.status == GKDYVideoPlayerStatusPaused){
        [_vedioPlayer resumePlay];
    }
}



- (void)player:(GKDYVideoPlayer *)player statusChanged:(GKDYVideoPlayerStatus)status{
    NSInteger index = _itemIndex - itemBaseTag;
    if(index < 0){
        return;
    }
    RGMeDynamicsCell *cell = (RGMeDynamicsCell *)[self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForRow:(_itemIndex - itemBaseTag) inSection:1]];
    if([cell isKindOfClass:[RGMeDynamicsCell class]]){
        if(_vedioPlayer.status == GKDYVideoPlayerStatusPlaying){
            cell.isShowPlayBtn = NO;
        }else if (_vedioPlayer.status == GKDYVideoPlayerStatusPaused){
            cell.isShowPlayBtn = YES;
        }
    }
   
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if(_tabIndex != 1){
        return;
    }
    // 将collectionView在控制器view的中心点转化成collectionView上的坐标
    CGPoint pInView = [self convertPoint:self.collectionView.center toView:self.collectionView];
    NSIndexPath *path = [_collectionView indexPathForItemAtPoint:pInView];
    RGMeDynamicsCell  *cell = (RGMeDynamicsCell *)[_collectionView cellForItemAtIndexPath:path];
    if((cell != nil) && [cell isKindOfClass:[RGMeDynamicsCell class]]){
        if(cell.tag == _itemIndex){//表示是当前
          
        }else{
            _itemIndex = cell.tag;
            [_vedioPlayer removeVideo];
            [self.vedioPlayer playVideoWithView:cell.vedioGroudView url:cell.vedioUrl];
            if([self.meViewDelegate respondsToSelector:@selector(updateVedioPlayCountWithVedioId:)]){
                [self.meViewDelegate updateVedioPlayCountWithVedioId:cell.vedioModel.ID];
            }
        }
    }
}



//UIScrollViewDelegate Delegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat offsetY = scrollView.contentOffset.y;
    if (offsetY < 0) {
        [_userInfoHeader overScrollAction:offsetY];
    }else {
        [_userInfoHeader scrollToTopAction:offsetY];
//        [self.meViewDelegate updateNavigationTitle:offsetY];
        CGFloat alphaRatio = offsetY/(330.0f - 44 - kStatusBarHeight);
        //    NSLog(@"ratio + %f",alphaRatio);
        //    _containerView.alpha =  alphaRatio;
        _customNavTitleLabel.textColor = UICOLOR_WITH_RGBINT_A(0xffffff, alphaRatio);
    }
}

#pragma mark - public
-(void) startFavoriteAnimationWithIsFavorite:(BOOL)isFavorite cellIndex:(NSInteger)index{
    if(_tabIndex != 1){
        return;
    }
    RGMeDynamicsCell *cell = (RGMeDynamicsCell *)[self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForRow:index inSection:1]];
    if([cell isKindOfClass:[RGMeDynamicsCell class]] && cell != nil){
        [cell updateFaroviteStauts];
    }
}

-(void) updateCommentCountsWhenCommentSuccessWithCellIndex:(NSInteger)cellIndex{
    if(_tabIndex != 1){
        return;
    }
    RGMeDynamicsCell *cell = (RGMeDynamicsCell *)[self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForRow:cellIndex inSection:1]];
    if([cell isKindOfClass:[RGMeDynamicsCell class]] && cell != nil){
        [cell updateCommentCounts];
    }
}
//转发成功后更新转发数
-(void)updateforwardCountsWhenShareSuccessWithCellIndex:(NSInteger)cellIndex{
    if(_tabIndex != 1){
        return;
    }
    RGMeDynamicsCell *cell = (RGMeDynamicsCell *)[self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForRow:cellIndex inSection:1]];
    if([cell isKindOfClass:[RGMeDynamicsCell class]] && cell != nil){
        [cell updateForwardCounts];
    }
}

-(void)reloadData{
    [_collectionView reloadData];
}


#pragma mark - reponse

- (void)actionBtnClick:(UIButton *)btn {
    if (self.meViewDelegate && [self.meViewDelegate respondsToSelector:@selector(actionBtnClickWithView:Tag:)]) {
        [self.meViewDelegate actionBtnClickWithView:self Tag:btn.tag];
    }
}

#pragma mark - getter
-(UICollectionView *)collectionView{
    if(!_collectionView){
        HoverViewFlowLayout *layout = [[HoverViewFlowLayout alloc] initWithTopHeight:SafeAreaTopHeight + kSlideTabBarHeight];
        layout.minimumLineSpacing = 1;
        layout.minimumInteritemSpacing = 0;
        _collectionView = [[UICollectionView  alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
        if (@available(iOS 11.0, *)) {
            _collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        } else {
//            self.automaticallyAdjustsScrollViewInsets = NO;
        }
        
        _collectionView.alwaysBounceVertical = YES;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        [_collectionView registerClass:[RGUserInfoHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"RGUserInfoHeaderView"];
        [_collectionView registerClass:[RGMeVedioCell class] forCellWithReuseIdentifier:@"RGMeVedioCell"];
        [_collectionView registerClass:[RGMeDynamicsCell class] forCellWithReuseIdentifier:@"RGMeDynamicsCell"];
    }
    return _collectionView;
}

-(UILabel *)customNavTitleLabel{
    if(!_customNavTitleLabel){
        _customNavTitleLabel = [[UILabel alloc]init];
        _customNavTitleLabel.font = FONT(customBoldFontName, FFont_17);
        _customNavTitleLabel.textColor = UICOLOR_WITH_RGBINT_A(0xffffff, 0.0);
        _customNavTitleLabel.textAlignment = NSTextAlignmentCenter;
        _customNavTitleLabel.text = self.model.nickname;
    }
    return _customNavTitleLabel;
}

- (NSMutableArray *)worksArray {
    if (!_worksArray) {
        _worksArray = [NSMutableArray new];
    }
    return _worksArray;
}

- (NSMutableArray *)dynamicsArray {
    if (!_dynamicsArray) {
        _dynamicsArray = [NSMutableArray new];
    }
    return _dynamicsArray;
}

- (NSMutableArray *)loversArray {
    if (!_loversArray) {
        _loversArray = [NSMutableArray new];
    }
    return _loversArray;
}

-(GKDYVideoPlayer *)vedioPlayer{
    if(!_vedioPlayer){
        _vedioPlayer = [[GKDYVideoPlayer alloc]init];
        _vedioPlayer.delegate = self;
    }
    return _vedioPlayer;
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
